package com.beyond.trace.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.storage.Decrease;
import com.beyond.trace.domain.storage.Increase;
import com.beyond.trace.requestVo.ProduceQuery;
import com.beyond.trace.service.storage.IIncreaseService;
import com.beyond.trace.service.storage.IProduceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 入库信息记录
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/increase")
@Api(value = "入库信息记录", tags = "入库信息记录")
public class IncreaseController extends BaseController
{
    @Autowired
    private IIncreaseService increaseService;

    @GetMapping("/getIncreasePage")
    @ApiOperation("分页入库信息记录")
    public TableDataInfo getIncreasePage(Increase increase)
    {
        startPage();
        QueryWrapper<Increase> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(increase.getIncreaseNo())) {
            queryWrapper.like("increase_no",increase.getIncreaseNo());
        }
        if(StringUtils.isNotEmpty(increase.getIncreaseName())) {
            queryWrapper.like("increase_name",increase.getIncreaseName());
        }
        if(StringUtils.isNotEmpty(increase.getIncreasePerson())) {
            queryWrapper.like("increase_person",increase.getIncreasePerson());
        }
        if(StringUtils.isNotNull(increase.getIncreaseTime())) {
            queryWrapper.eq("increase_time",increase.getIncreaseTime());
        }
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        queryWrapper.orderByDesc("create_time");
        List<Increase> list = increaseService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping(value = "/getIncreaseById")
    @ApiOperation("通过id查询入库信息记录")
    public AjaxResult getIncreaseById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        if(increaseService.getById(id) == null) {
            return AjaxResult.error("没有查到信息");
        }
        QueryWrapper<Increase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return AjaxResult.success(increaseService.getOne(queryWrapper));
    }

    @PostMapping(value = "/addIncrease")
    @ApiOperation("添加入库信息记录")
    public AjaxResult addIncrease(@RequestBody @Validated Increase increase)
    {
        increase.setCreateBy(SecurityUtils.getUsername());
        increase.setUpdateBy(SecurityUtils.getUsername());
        increase.setCreateTime(new Date());
        increase.setUpdateTime(new Date());
        if(StringUtils.isEmpty(increase.getIncreaseNo())) {
            increase.setIncreaseNo(SnowUtil.getId());
        }
        increase.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        return AjaxResult.success(increaseService.save(increase));
    }

    @PostMapping(value = "/editIncrease")
    @ApiOperation("修改入库信息记录")
    public AjaxResult editIncrease(@RequestBody Increase increase)
    {
        if(StringUtils.isEmpty(increase.getId())) {
            return AjaxResult.error("id不能为空");
        }
        increase.setUpdateBy(SecurityUtils.getUsername());
        increase.setUpdateTime(new Date());
        return AjaxResult.success(increaseService.updateById(increase));
    }

    @GetMapping(value = "/deleteIncreaseById")
    @ApiOperation("删除入库信息记录")
    public AjaxResult deleteIncreaseById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        List<String> list = Arrays.asList(id.split(","));
        QueryWrapper<Increase> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",list);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return AjaxResult.success(increaseService.remove(queryWrapper));
    }



}
